package timing.ukulele.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.project.persistent.Dictionary;
import timing.ukulele.project.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/dictionary")
@Api("字典接口")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    public DictionaryController(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @GetMapping("/page")
    @ApiOperation("分页获取字典列表")
    public ResponseResult<IPage<Dictionary>> page(
            @ApiParam(value = "查询名称", required = true) @RequestParam String name,
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
            @ApiParam(value = "系统权限ID") @RequestHeader(Constant.SYSTEM) String systemIds) {
        return ResponseResult.success(dictionaryService.getPage(name, systemIds, pageNum, pageSize, new HashMap<>()));
    }

    @PostMapping("/create")
    @ApiOperation("保存字典信息")
    @ApiImplicitParam(name = "dictionary", value = "业务系统对象", required = true, dataType = "Dictionary")
    public ResponseResult<Dictionary> create(
            @ApiParam(value = "字典", required = true) @RequestBody Dictionary dictionary,
            @RequestHeader(Constant.USERNAME) String username) {
        dictionary.setCreateBy(username);
        dictionaryService.save(dictionary);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改字典信息")
    @ApiImplicitParam(name = "dictionary", value = "业务系统对象", required = true, dataType = "Dictionary")
    public ResponseResult<Dictionary> update(
            @ApiParam(value = "字典", required = true) @RequestBody Dictionary dictionary,
            @RequestHeader(Constant.USERNAME) String username) {
        dictionary.setUpdateBy(username);
        dictionaryService.updateById(dictionary);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除字典")
    public ResponseResult<Dictionary> remove(@ApiParam(value = "字典id", required = true, example = "1") @RequestParam Long id) {
        dictionaryService.removeById(id);
        return ResponseResult.success();
    }

}
